// import bcrypt from 'bcryptjs'
import jwt from 'jsonwebtoken'
import { jwtConfig } from '../config/index'
// 密码加密
const adminUser = () => {
	return async (ctx, next) => {
		// 检查请求的接口是否admin
		// 检查token是否符合
		// 检查密码是否符合
		const outCheck = /\/dlyadminapi\/*/
		if (!outCheck.test(ctx.url)) return await next()
        const outCheckAdmin = [/\/dlyadminapi\/link\/cheackUserProxy\/*/]
        if(outCheckAdmin.filter((item) => item.test(ctx.url)).length) return await next()
		// 如果是后台接口 判断是否为后台jwt
		if (ctx.header.authorization) {
			const jwtstr = ctx.header.authorization.split('Bearer ')[1]
			const jwtdata = await jwt.verify(jwtstr, jwtConfig.secret)
			if (jwtdata.phone && jwtdata.password)  {
                ctx.request.header.jwtuserinfo = jwtdata
                return await next()
            }
			ctx.body = {
				code: 747,
				msg: '请登录',
			}
			return
		}
		await next()
	}
}
export default adminUser
